/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.database;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import lineage.config.L1Config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 数据库工厂类.
 * 
 * @version 2014年3月12日下午6:35:23
 * @author jrwz
 */
public final class L1DatabaseFactory {
    private static final Logger LOG = LoggerFactory.getLogger(L1DatabaseFactory.class);
    /** 数据库连接. */
    private ComboPooledDataSource source;

    /**
     * 静态初始化器，由JVM来保证线程安全.
     */
    private static class Holder {
        static L1DatabaseFactory instance = new L1DatabaseFactory();
    }

    /**
     * 取得该类的实例.
     */
    public static L1DatabaseFactory getInstance() {
        return Holder.instance;
    }

    private L1DatabaseFactory() {
        try {
            source = new ComboPooledDataSource();
            source.setDriverClass(L1Config.DB_DRIVER); // 驱动
            source.setJdbcUrl(L1Config.DB_URL); // url
            source.setUser(L1Config.DB_LOGIN); // 数据库账号
            source.setPassword(L1Config.DB_PASSWORD); // 数据库密码

            source.getConnection().close();
        } catch (PropertyVetoException e) {
            LOG.error(e.getLocalizedMessage(), e);
        } catch (SQLException e) {
            int error = e.getErrorCode();
            String msg = null;
            switch (error) {
            case 0:
                msg = "驱动程序错误";
                break;
            case 1045:
                msg = "用户名或密码错误";
                break;
            case 1049:
                msg = "数据库错误";
                break;
            case 1130:
                msg = "远程连接错误";
                break;
            default:
                msg = "SQLException";
                break;
            }
            LOG.error(msg, e);
        } catch (Exception e) {
            LOG.error("初始化数据库连接时发生错误", e);
        }
    }

    /**
     * 取得数据库连接时的连线.
     * 
     * @return 连线
     */
    public Connection getConnection() {
        Connection con = null;

        while (con == null) {
            try {
                con = source.getConnection();
            } catch (SQLException e) {
                LOG.warn("数据库连线时发生错误,自动重试...", e);
            }
        }
        return con;
    }

    /**
     * 关闭.
     */
    public void close() {
        try {
            if (source != null) {
                source.close();
            }
        } catch (final Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        } finally {
            try {
                if (source != null) {
                    source = null;
                }
            } catch (final Exception e) {
                LOG.error(e.getLocalizedMessage(), e);
            }
        }
    }
}
